﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Text;
using Common;

namespace webs.website_admin.hr
{
    public partial class jobseditor : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Response.Expires = -1;
            PageBegin();

            /// 保存设置
            if (RequestBase.GetFormString("___EVENTTARGET") == "save")
            {
                Save();
            }

            if (!IsPostBack)
            {
                CategoryDataBind();
                EnumDataBind();
                if (RequestBase.GetQueryString("objectid") != "")
                    Initialize(); 
            }
        }

        #region 下来菜单数据绑定
        void CategoryDataBind()
        {
            DataTable dt = BLL.CommonBLL.Select("KbCategoryBase", "*", " AND DeletionStateCode = 0 ORDER BY LineNum ASC");
            if (dt != null && dt.Rows.Count > 0)
            {
                selCategoryList.Items.Add(new ListItem("\t+\t" + dt.Select("CategoryNumber = '" + RequestBase.GetQueryString("c_number") + "'")[0]["CategoryName"].ToString(), dt.Select("CategoryNumber = '" + RequestBase.GetQueryString("c_number") + "'")[0]["CategoryId"].ToString()));
                DropdownlistDataBind(2, dt.Select("CategoryNumber = '" + RequestBase.GetQueryString("c_number") + "'")[0]["CategoryId"].ToString(), dt);
            }
            else
                DropdownlistDataBind(1, "00000000-0000-0000-0000-000000000000", dt);
        }
        /// <summary>
        /// 类别列表邦定
        /// </summary>
        /// <param name="intLayer">级别</param>
        /// <param name="parentId">父目录ID</param>
        /// <param name="dt">目录数据表</param>
        void DropdownlistDataBind(int intLayer, string parentId, DataTable dt)
        {
            if (dt == null)
                return;

            DataRow[] dr = dt.Select("Layer = " + intLayer.ToString() + " AND ParentId = '" + parentId + "'");

            ArrayList al = new ArrayList();
            ListItem li;

            string str = ""; int intCategoryLayerID = 0;
            foreach (DataRow d in dr)
            {
                str = "　| ";
                intCategoryLayerID = int.Parse(d["Layer"].ToString());
                if (intLayer == 1)
                    str = " + ";
                for (int i = 1; i < intLayer * intLayer; i++)
                    str += "--";

                li = new ListItem("\t" + str + "\t" + d["CategoryName"].ToString(), d["CategoryId"].ToString());
                selCategoryList.Items.Add(li);
                DropdownlistDataBind(intCategoryLayerID + 1, d["CategoryId"].ToString().Trim(), dt);
            }
        }


        void EnumDataBind()
        {
            DataTable dat = BLL.Search.CommonSearch.ExecSelect("EnumItem",
                 new string[] { "[EnumKey]" },
                 new string[] { "Equal" },
                 new string[] { "'JobArea'" },
                 null,
                 null);
 
            foreach (DataRow dr in dat.Select(""))
            {
                selWorkAddressTitle.Items.Add(new ListItem(dr["EnumText"].ToString().Trim(), dr["EnumValue"].ToString().Trim()));
            }
            dat.Dispose();

            dat = BLL.Search.CommonSearch.ExecSelect("EnumItem",
                 new string[] { "[EnumKey]" },
                 new string[] { "Equal" },
                 new string[] { "'JobType'" },
                 null,
                 null);
 
            foreach (DataRow dr in dat.Select(""))
            {
                selJobType.Items.Add(new ListItem(dr["EnumText"].ToString().Trim(), dr["EnumValue"].ToString().Trim()));
            }
            dat.Dispose(); 
        }
        #endregion

        #region 数据初始化
        void Initialize()
        {
            Model.HR.JobsBase _obj = BLL.HR.JobsBase.GetObjById(RequestBase.GetQueryString("objectid"));

            ArticleObj = _obj;
        }

        Model.HR.JobsBase ArticleObj
        {
            get
            {
                Model.HR.JobsBase _article = new Model.HR.JobsBase();

                if (!string.IsNullOrEmpty(RequestBase.GetQueryString("objectid")))
                    _article.JobId = RequestBase.GetQueryString("objectid");
  
                _article.JobTitle = this.txtJobTitle.Value.Trim();
                _article.WorkAddress = this.txtWorkAddress.Value.Trim();
                _article.PeopleNum = this.txtPeopleNum.Value.Trim();
                _article.EducationAbility = this.txtEducationAbility.Value.Trim();
                _article.ProfessionalAbility = this.txtProfessionalAbility.Value.Trim();
                _article.ComputerAbility = this.txtComputerAbility.Value.Trim();
                _article.EnglishAbility = this.txtEnglishAbility.Value.Trim();
                _article.ExperienceReq = this.txtExperienceReq.Value.Trim();
                _article.SexReq = this.txtSexReq.Value.Trim();
                _article.AgeReq = this.txtAgeReq.Value.Trim();
                _article.OthersReq = this.txtOthersReq.Value.Trim();
                _article.HomePlaceReq = this.txtHomePlaceReq.Value.Trim();
                _article.LineNum = decimal.Parse(this.txtLineNum.Value.Trim());
                _article.CategoryId = this.selCategoryList.Value.Trim();
                _article.WorkAddressCode = this.selWorkAddressTitle.Value.Trim();
                _article.WorkAddressTitle = this.txtWorkAddressCode.Value.Trim();
                _article.JobTypeCode = this.selJobType.Value.Trim();
                _article.JobType = this.txtJobTypeCode.Value.Trim();
                _article.PublishDate = DateTime.Parse(this.txtPublishDate.Value.Trim());
                _article.CreatedBy = this.SystemUserId;
                _article.DutyDescription = txtDutyDescription.Value.Trim();
                 
                return _article;
            }
            set
            {
                if (value != null)
                { 
                    this.txtJobTitle.Value = value.JobTitle;
                    this.txtWorkAddress.Value = value.WorkAddress;
                    this.txtPeopleNum.Value = value.PeopleNum;
                    this.txtEducationAbility.Value = value.EducationAbility;
                    this.txtProfessionalAbility.Value = value.ProfessionalAbility;
                    this.txtComputerAbility.Value = value.ComputerAbility;
                    this.txtEnglishAbility.Value = value.EnglishAbility;
                    this.txtExperienceReq.Value = value.ExperienceReq;
                    this.txtSexReq.Value = value.SexReq;
                    this.txtAgeReq.Value = value.AgeReq;
                    this.txtOthersReq.Value = value.OthersReq;
                    this.txtHomePlaceReq.Value = value.HomePlaceReq;
                    this.txtLineNum.Value = value.LineNum.ToString();
                    this.selCategoryList.Value = value.CategoryId;
                    this.selWorkAddressTitle.Value = value.WorkAddressCode;
                    this.selJobType.Value = value.JobTypeCode; 
                    this.txtPublishDate.Value = value.PublishDate.ToString();
                    this.txtWorkAddressCode.Value = value.WorkAddressTitle;
                    this.txtJobTypeCode.Value = value.JobType;
                    this.txtDutyDescription.Value = value.DutyDescription;
                }
            }
        }
        #endregion

        #region 保存数据
        void Save()
        {
            string res = "";

            if (RequestBase.GetQueryString("otype").Trim() == "add")
            {
                res = BLL.HR.JobsBase.Add(ArticleObj);
            }

            if (RequestBase.GetQueryString("otype").Trim() == "modify")
            {
                res = BLL.HR.JobsBase.Modify(ArticleObj);
            }

            switch (res.Split('|')[0])
            {
                case "succeeded":
                    JScript.AlertAndReturnValueToParent("信息保存成功!", "window.opener.LoadingData(); window.close();");
                    break;
                default:
                    JScript.Alert(res);
                    break;
            }
        }
        #endregion
    }
}
